package com.alliky.cart.ui.viewmodel;

import android.app.Application;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableList;

import com.alliky.cart.BR;
import com.alliky.cart.R;
import com.alliky.core.base.viewmodel.ToolbarViewModel;
import com.alliky.core.binding.command.BindingCommand;
import com.alliky.core.bus.SingleLiveEvent;
import com.alliky.core.callback.OnItemClickListener;
import com.alliky.core.data.DataRepository;
import com.alliky.core.entity.vo.GoodsVO;
import com.alliky.core.net.IDisposableObserver;
import com.alliky.core.util.RxToast;
import com.alliky.core.util.RxUtils;

import java.util.List;
import java.util.WeakHashMap;

import me.tatarka.bindingcollectionadapter2.ItemBinding;

/**
 * @Description TODO
 * @Author wxianing
 * @Date 2020-11-15
 */
public class CartViewModel extends ToolbarViewModel<DataRepository> {

    private int pageNum = 1;
    private int pageSize = 12;

    public CartViewModel(@NonNull Application application) {
        super(application);
    }

    public void initToolbar() {
        setBackIconVisible(View.GONE);
        setTitleText("购物车");
    }

    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();

    public class UIChangeObservable {
        //下拉刷新完成
        public SingleLiveEvent finishRefresh = new SingleLiveEvent<>();
        //上拉加载完成
        public SingleLiveEvent finishLoadMore = new SingleLiveEvent<>();
    }

    public OnItemClickListener mListener = (view, obj) -> {
        GoodsVO goodsVO = (GoodsVO) obj;

        RxToast.info(goodsVO.getGoods_name(), 1);

//        new RxDialog.Builder(getContext())
//                .whitTitle("温馨提示")
//                .whitContent("您确定要删除" + goodsVO.getGoods_name() + "?")
//                .setPositiveListener(dialog -> {
//                    RxToast.normal("点击了确定");
//                    dialog.dismiss();
//                })
//                .build()
//                .show();
    };


    public ObservableList<RecommendItemViewModel> obserableRecommendList = new ObservableArrayList<>();

    public ItemBinding<Object> mItemBinding = ItemBinding.of(BR.viewModel, R.layout.item_cart_recommend_layout)
            .bindExtra(BR.mListener, mListener);


    //下拉刷新
    public BindingCommand onRefreshCommand = new BindingCommand(() -> {
        pageNum = 1;
        getRecommendGoods();
    });
    //上拉加载
    public BindingCommand onLoadMoreCommand = new BindingCommand(() -> {
        pageNum++;
        getRecommendGoods();
    });

    public void initData() {
        getRecommendGoods();
    }

    public void getRecommendGoods() {
        WeakHashMap<String, Object> param = new WeakHashMap<>();
        param.put("pageNum", pageNum);
        param.put("pageSize", pageSize);
        mRepository.getRecommendGoods(param)
                .compose(RxUtils.bindToLifecycle(getLifecycleProvider()))// 请求与View周期同步
                .compose(RxUtils.schedulersTransformer())// 线程调度
                .compose(RxUtils.exceptionTransformer())// 网络错误的异常转换
                .doOnSubscribe(this)
                .subscribe(new IDisposableObserver<List<GoodsVO>>(null) {
                    @Override
                    protected void onResult(List<GoodsVO> goodsVOS) {
                        if (pageNum == 1) {
                            obserableRecommendList.clear();
                            uc.finishRefresh.call();
                        } else {
                            uc.finishLoadMore.call();
                        }
                        for (GoodsVO goodsVO : goodsVOS) {
                            RecommendItemViewModel viewModel = new RecommendItemViewModel(CartViewModel.this, goodsVO);
                            obserableRecommendList.add(viewModel);
                        }
                    }
                });
    }
}